<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>bubble sort</title>
</head>
<body>
<script>
    (function bubble(arr) {
        let temp;
        for (let i = 0; i < arr.length; i++) {
            for (let j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        console.log(arr);
    })([1, 2, 1, 12, 1, 51, 31])

    //变量赋值的时候,若为类型,则复制的是指针,若为数值,则赋值数值
    //函数传参的时候,若有形参,则为复制,若无形参,则为变量传入
    function bubble2(arr) {
        var arrS = [];
        var array = arr.slice();
        for (let i = 0; i < arr.length; i++) {
            let max = Math.max.apply(Math, array);
            arrS.push(max);
            for (let j = 0; j < array.length; j++) {
                if (array[j] === max) {
                    array.splice(j, 1);
                    break;
                }
            }
        }
        return arrS;
    }
    console.log(bubble2([1, 2, 1, 12, 1, 51, 31]));

    var x = function () {};
    console.log(x);
</script>
</body>
</html>